import create from "zustand";
import { devtools, persist } from "zustand/middleware";

import { STORAGE_NAMES } from "~/modules/storage/storage-names";

interface CustomParams {
  id: string;
  name: string;
  desc: string;
  expression: string;
}
export interface TaskParam {
  name: string;
  quality: string[];
  desc: string;
}
export interface Task {
  id: string;
  name: string;
  startTime: string;
  endTime: string;
  aircraftId: string;
  tailno: string;
  flights: string[];
  type?: string;
  customParams: CustomParams[];
  taskParams: TaskParam[];
}
export interface TaskState {
  analysisTask: Task;
}

export interface TaskAction {
  updateTask: (analysisTask: Task) => void;
}

const initState = (): TaskState => {
  return {
    analysisTask: {
      id: "",
      name: "",
      startTime: "",
      endTime: "",
      aircraftId: "",
      tailno: "",
      flights: [],
      type: "",
      customParams: [],
      taskParams: [],
    },
  };
};

export const useAnalysisTaskStore = create<TaskState & TaskAction>(
  // @ts-ignore
  devtools(
    persist(
      (set, get, api) => ({
        ...initState(),
        updateTask: (analysisTask: Task) => {
          set({ analysisTask });
        },
      }),
      {
        name: STORAGE_NAMES.TASKAPI.name,
        version: STORAGE_NAMES.TASKAPI.version,
      }
    )
  )
);

export const useAnalysisTask = () => {
  const { analysisTask, updateTask } = useAnalysisTaskStore();
  return {
    analysisTask,
    updateTask,
  };
};
